﻿@{
    ViewBag.Title = "来聊天啊";
}


<div class="container" style="margin-top: 100px;">
    先把账号和connectionid进行关联，然后通过账号（也就是系统的userid）发送到指定的用户<br/>
    账号：
    <input type="text" id="glt"/>
    <input type="button" id="gl" value="关联"/><br/>
    发送给：
    <input type="text" id="fsdt"/><br />
    消息内容：
    <input type="text" id="message"/>
    <input type="button" id="sendmessage" value="发送"/><br />
    <input type="hidden" id="displayname"/>
    日志输出：<ul id="discussion"></ul>
</div>

@section scripts
{
    <!--引用SignalR库. -->
    <script src="~/Scripts/jquery.signalR-2.4.1.min.js"></script>
    <!--引用自动生成的SignalR 集线器(Hub)脚本.在运行的时候在浏览器的Source下可看到 -->
    @*<script src="~/signalr/hubs"></script>*@

    <script>
        $(function () {
            // 1、引用自动生成的集线器代理 必须用小写字母开头
            //var chat = $.connection.chatHub;

            var listener = $.hubConnection('http://localhost:8239/MessageBus', { useDefaultPath: false });
            var hub = listener.createHubProxy('ChatHub');

            //// 2、开始连接服务器 done函数表明建立连接成功后为发送按钮注册了一个click事件
            //$.connection.hub.start().done(function () {
            //    $('#sendmessage').click(function () {
            //        // 调用服务器端集线器的Send方法 也要小写开头
            //        chat.server.send($('#message').val());
            //        // 清空输入框信息并获取焦点
            //        $('#message').val('').focus();
            //    });
            //});

            //// 3、定义服务器端调用的客户端sendMessage来显示新消息
            //chat.client.sendMessage = function (name, message) {
            //    // 向页面添加消息
            //    $('#discussion').append('<li><strong>' + htmlEncode(name)
            //        + '</strong>: ' + htmlEncode(message) + '</li>');
            //};

            //开启连接
            function startConnection() {
                hub.on('sendMessage', function (name, message) {

                    $('#discussion').append('<li>' + name + "----" + message + '</li>');
                });

                listener.start().fail(function () {
                    $('#discussion').append('<li>打开连接失败!</li>');
                }).done(function () {
                    //链接服务器成功
                    //hub.invoke('Addtoken', msg);
                    $('#discussion').append('<li>连接已打开...</li>');
                });

            }

            //断开连接
            function stopConnection() {
                listener.stop();
                $('#discussion').append('<li>连接已断开...</li>');
            };

            //发送消息
            $('#sendmessage').click(function () {
                var msg = $('#message').val();
                var f = $('#fsdt').val();
                console.log("f", f);
                hub.invoke('send', f, msg);
                $('#message').val('');
            });

            //发送消息
            $('#gl').click(function () {
                var msg = $('#glt').val();
                hub.invoke('Addtoken', msg);
                $('#message').val('');
            });


            startConnection();

            // 设置焦点到输入框
            $('#message').focus();
        });

        // 为显示的消息进行Html编码
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}

@*<div class="jumbotron">
        <h1>ASP.NET1111111111111111111111</h1>
        <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
        <p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
    </div>

    <div class="row">
        <div class="col-md-4">
            <h2>Getting started</h2>
            <p>
                ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
                enables a clean separation of concerns and gives you full control over markup
                for enjoyable, agile development.
            </p>
            <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
        </div>
        <div class="col-md-4">
            <h2>Get more libraries</h2>
            <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
            <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
        </div>
        <div class="col-md-4">
            <h2>Web Hosting</h2>
            <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
            <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
        </div>
    </div>*@